/*-
 * #%L
 * Utility methods for BoneJ2
 * %%
 * Copyright (C) 2015 - 2023 Michael Doube, BoneJ developers
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */


package org.bonej.utilities;

import ij.gui.Roi;
import ij.plugin.frame.RoiManager;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.joml.Vector3d;

/**
 * A class containing utility methods for the ImageJ RoiManager
 *
 * @author Michael Doube
 * @author Richard Domander
 */
public final class RoiManagerUtil {
	private RoiManagerUtil() {}

	/**
	 * Checks if a ROI is active on all slices.
	 *
	 * @param sliceNumber the slice number or z-position of the ROI.
	 * @return true if the ROI is not associated with a particular slide.
	 */
	public static boolean isActiveOnAllSlices(final int sliceNumber) {
		return sliceNumber <= 0;
	}

	/**
	 * Gets the coordinates of all point ROIs in the manager.
	 * <p>
	 * NB z-coordinates start from 1. If a ROI is not associated with a slice,
	 * it's z = 0.
	 * </p>
	 *
	 * @param manager an instance of {@link RoiManager}.
	 * @return point ROI coordinates.
	 */
	public static List<Vector3d> pointROICoordinates(final RoiManager manager) {
		final Roi[] rois = manager.getRoisAsArray();
		return Arrays.stream(rois).filter(roi -> roi.getType() == Roi.POINT).map(
			roi -> {
				final int z = roi.getZPosition();
				return Arrays.stream(roi.getContainedPoints()).distinct().map(
					p -> new Vector3d(p.x, p.y, z));
			}).flatMap(s -> s).distinct().collect(Collectors.toList());
	}
}
